<?php

// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2030 http://www.jmzh1688.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: lxl
// +----------------------------------------------------------------------


namespace app\admin\controller;

use think\Controller;
use think\Db;

use app\admin\model\SysUserModel;

use app\admin\model\SysRoleModel;

class Sysuser extends Base
{


    public function index()
    {


        if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $usermodel = new SysUserModel();


            $where = getWhereForm( $param);
            $where['isdelete']=0;

            $list = $usermodel->getByWhere($where, $offset, $limit);
            foreach($list as $key=>$vo){
                $list[$key]['operate'] = $this->getRowButton($vo['id']);
            }
            $result['total'] = $usermodel->getAllCount($where);  // 总数据
            $result['rows'] = $list;
            return json($result);
        }
        return $this->fetch();

    }

    public function del()
    {
        $rolemodel = new SysUserModel();
        if(request()->isPost()){
            $id = input("post.id");
            $msg = $rolemodel->delById($id);
            return json(msg($msg['code'], $msg['data'], $msg['msg']));
        }
        return $this->fetch();
    }

    public function edit()
    {
        $usermodel = new SysUserModel();

        if(request()->isPost()){
            $param = input("post.");
            $msg = $usermodel->edit($param);
            return json(msg($msg['code'], $msg['data'], $msg['msg']));
        }
      
        $id = input('param.id');
        $info=$usermodel->getById($id);

        $rolemodule=new SysRoleModel();
        $rolelist=$rolemodule->getAllList();


        $this->assign([
            'info' => $info,
            'rolelist' => $rolelist
        ]);
        return $this->fetch();
    }

    public function add()
    {

        if(request()->isPost()){
            $rolemodel = new SysUserModel();
            $param = input("post.");
            $msg = $rolemodel->add($param);
            return json(msg($msg['code'], $msg['data'], $msg['msg']));
        }
    
        return $this->fetch();
    }

    
    /**
     * 操作按钮
     * @param $id
     */
    private function getRowButton($id)
    {
        if($id==1){return "";}
        $buttonlist= " <a href='edit?id=".$id."' class='btn btn-outline-primary btn-sm mod'>编辑</a> "
        ." <button class='btn btn-outline-primary btn-sm' onclick='del(this,".$id.");' >删除</button> ";
       return $buttonlist;
    }

    /**
     * <layui 改版> 获取管理员数据列表
     * @return mixed
     */
    public function ajaxGetSysuser(){
        $model = new SysUserModel();

        if(request()->isAjax()){
            $param = request()->param();

            $result = $model->getSysUser($param);
            return $result;
        }
    }

    /**
     * <layui 改版> 获取角色列表
     * @return mixed
     */
    public function ajaxGetSysRole(){
        $rolemodule=new SysRoleModel();
        $rolelist=$rolemodule->getAllList();
        if($rolelist){
            $ret['data'] = $rolelist;
            $ret['code'] = 0;
            $ret['msg']  = '获取数据成功';
        }else{
            $ret['data'] = '';
            $ret['code'] = -1;
            $ret['msg']  = '获取数据为空';
        }

        return $ret;
    }

    /**
     * <layui 改版> 保存管理员数据
     * @return mixed
     */
    public function ajaxSaveSysuser(){
        $model = new SysUserModel();
        if(request()->isAjax()){
            $param = request()->param();
            $result = $model->ajaxSaveData($param);
            return $result;
        }
    }
}
